package com.config.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.config.entity.ChargeConfig;
import com.config.service.ChargeConfigService;


@Controller
@RequestMapping("/chargeConfig")
public class ChargeConfigController {

	@Resource(name="chargeConfigService")
	private ChargeConfigService chargeconfigService;
	
	/**
	 * 状态主界面
	 * @return
	 */
	@RequestMapping("/chargeConfigMain")
	public String stateMain(Model model){
		model.addAttribute("currentName","电费模式配置");
		return "config/chargeConfig";
	}
	
	/**
	 * treegrid的分页显示
	 * @param model
	 * @param id
	 * @param start
	 * @param limit
	 * @return
	 */
	@RequestMapping("/findByTreePage")
	public Model findByTreePage(Model model,
			@RequestParam(value="id",required=false)Integer id,
			@RequestParam(value = "page", required = false, defaultValue="0")int start,
			@RequestParam(value = "rows", required = false, defaultValue="10")int limit){
		List<ChargeConfig> list=chargeconfigService.findByTreePage(id, start, limit);
		int total=chargeconfigService.countByTreePage(id);
		model.addAttribute("rows",list);
		model.addAttribute("total",total);
		return model;
	}
	
	@RequestMapping("/save")
	public Model save(Model model,ChargeConfig entity){
		int sequence=0;
		SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm" );
		if(entity.getFatherId()==null){
			entity.setIsEffective("N");
			sequence=chargeconfigService.countByTreePage(null);
		}else{
			List<ChargeConfig> list=chargeconfigService.getCharges(entity.getFatherId());
			String start="2016-01-01 "+entity.getStart();
			String end="2016-01-01 "+entity.getEnd();
			try {
				Date st = sdf.parse(start);
				Date et = sdf.parse(end);
				if(list.size()>0){//判断后续添加的时间刻度是否从02号开始
					Date flag=list.get(0).getStartTime();
					if(flag.compareTo(st)<0){//添加的开始时刻从01号开始
						entity.setStartTime(st);
						if(st.compareTo(et)<0){
							entity.setEndTime(et);
						}else{
							entity.setEndTime(sdf.parse("2016-01-02 "+entity.getEnd()));
						}
					}else{
						entity.setStartTime(sdf.parse("2016-01-02 "+entity.getStart()));
						if(st.compareTo(et)<0){
							entity.setEndTime(sdf.parse("2016-01-02 "+entity.getEnd()));
						}else{
							Calendar   calendar   =   new   GregorianCalendar(); 
						     calendar.setTime(flag); 
						     calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动 
							entity.setEndTime(calendar.getTime());
						}
					}
				}else{
					if(st.compareTo(et)<0){//开始时间小于结束时间，正常
						entity.setStartTime(st);
						entity.setEndTime(et);
					}else{//开始时间 大于等于 结束时间， 设置结束时间为后一天
						entity.setStartTime(st);
						entity.setEndTime(sdf.parse("2016-01-02 "+entity.getEnd()));
					}
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
			sequence=chargeconfigService.countByTreePage(entity.getFatherId());
			entity.setChargeType("");
		}
		entity.setSequence(sequence+1);
		chargeconfigService.save(entity);
		model.addAttribute("success",true);
		model.addAttribute("msg","保存成功");
		return model;
	}
	
	@RequestMapping("/load")
	public Model load(Model model,Integer id){
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		ChargeConfig entity=chargeconfigService.getChargeConfig(id);
		if(entity.getStartTime()!=null&&entity.getEndTime()!=null){
			entity.setStart(sdf.format(entity.getStartTime()));
			entity.setEnd(sdf.format(entity.getEndTime()));
		}
		model.addAttribute("data",entity);
		model.addAttribute("success",true);
		model.addAttribute("msg","加载成功");
		return model;
	}
	
	@RequestMapping("/update")
	public Model update(Model model,ChargeConfig entity){
		SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm" );
		if(entity.getFatherId()==null){
			entity.setIsEffective("N");
		}else{
			List<ChargeConfig> list=chargeconfigService.getCharges(entity.getFatherId());
			String start="2016-01-01 "+entity.getStart();
			String end="2016-01-01 "+entity.getEnd();
			try {
				Date st = sdf.parse(start);
				Date et = sdf.parse(end);
				if(list.size()>1){//判断后续添加的时间刻度是否从02号开始
					Date flag=list.get(0).getStartTime();
					if(flag.compareTo(st)<0){//添加的开始时刻从01号开始
						entity.setStartTime(st);
						if(st.compareTo(et)<0){
							entity.setEndTime(et);
						}else{
							entity.setEndTime(sdf.parse("2016-01-02 "+entity.getEnd()));
						}
					}else{
						entity.setStartTime(sdf.parse("2016-01-02 "+entity.getStart()));
						if(st.compareTo(et)<0){
							entity.setEndTime(sdf.parse("2016-01-02 "+entity.getEnd()));
						}else{
							Calendar   calendar   =   new   GregorianCalendar(); 
						     calendar.setTime(flag); 
						     calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动 
							entity.setEndTime(calendar.getTime());
						}
					}
				}else{
					if(st.compareTo(et)<0){//开始时间小于结束时间，正常
						entity.setStartTime(st);
						entity.setEndTime(et);
					}else{//开始时间 大于等于 结束时间， 设置结束时间为后一天
						entity.setStartTime(st);
						entity.setEndTime(sdf.parse("2016-01-02 "+entity.getEnd()));
					}
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
			entity.setChargeType("");
		}
		chargeconfigService.update(entity);
		model.addAttribute("success",true);
		model.addAttribute("msg","修改成功");
		return model;
	}
	
	@RequestMapping("/delete")
	public Model delete(Model model,Integer id){
		//1.查询该记录是模式还是配置，
		ChargeConfig entity=chargeconfigService.getChargeConfig(id);
		if(entity.getFatherId()==null){//如果是模式
			if(entity.getIsEffective().equals("Y")){
				model.addAttribute("success",false);
				model.addAttribute("msg","不能删除有效的配置");
			}else{
				List<ChargeConfig> list=chargeconfigService.getCharges(id);
				if(list.size()>0){
					for(ChargeConfig e:list){
						chargeconfigService.delete(e.getId());
					}
				}
				chargeconfigService.delete(id);
				model.addAttribute("success",true);
				model.addAttribute("msg","模式和对应配置均删除成功");
				
			}
		}else{//如果是配置
			ChargeConfig e=chargeconfigService.getChargeConfig(entity.getFatherId());
			if(e.getIsEffective().equals("Y")){
				model.addAttribute("success",false);
				model.addAttribute("msg","不能删除有效的配置");
			}else{
				chargeconfigService.delete(id);
				model.addAttribute("success",true);
				model.addAttribute("msg","删除成功");
			}
		}
		return model;
	}
	
	
	@RequestMapping("/setEffective")
	public Model setEffective(Model model,Integer  id){
		boolean flag=chargeconfigService.checkVertherRepeat(id);
		if(flag){
			//1.将原来为Y的记录设置N
			ChargeConfig e=chargeconfigService.getEffectiveConfig();
			if(e!=null){
				e.setIsEffective("N");
				chargeconfigService.update(e);
			}
			//2.设置修改的记录为Y
			ChargeConfig entity = chargeconfigService.getChargeConfig(id);
			entity.setIsEffective("Y");
			chargeconfigService.update(entity);
			model.addAttribute("success",true);
			model.addAttribute("msg","修改成功");
		}else{
			model.addAttribute("success",false);
			model.addAttribute("msg","失效，请确定时刻无重复且满足24小时");
		}
		return model;
	}
	
	@RequestMapping("/setNoEffective")
	public Model setNoEffective(Model model,Integer  id){
		ChargeConfig entity = chargeconfigService.getChargeConfig(id);
		entity.setIsEffective("N");
		chargeconfigService.update(entity);
		model.addAttribute("success",true);
		model.addAttribute("msg","修改成功");
		return model;
	}
	
}
